package com.fast.linkbeanadmin.security.impl;

import com.fast.linkbeanadmin.enums.CodeEnum;
import com.fast.linkbeanadmin.pojo.response.ResultBean;
import com.fast.linkbeanadmin.utils.response.ResponseUtil;
import org.springframework.http.MediaType;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ruan cai yuan
 * @version 1.0
 * @fileName com.fast.linkbeanadmin.security.impl.AccessDeniedHandlerImpl
 * @description: ExceptionTranslationFilter出现访问拒绝异常AccessDeniedException时(403状态码)会回调handle方法【访问拒绝处理】
 * @since 2024/8/28 09:49
 */
@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        //无权访问
        ResultBean result = ResultBean.error(CodeEnum.METHOD_NOT_ALLOWED.getCode(), CodeEnum.METHOD_NOT_ALLOWED.getMsg());
        ResponseUtil.responseData(response, result, MediaType.APPLICATION_JSON_VALUE);
    }
}